package com.sw.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.sw.domain.FullActive;
import com.sw.domain.FullActivePage;
import com.sw.exception.InvalidArgumentException;
import com.sw.mapper.ActiveEnrollMapper;
import com.sw.mapper.ActiveMapper;
import com.sw.mapper.OriginatorMapper;
import com.sw.model.Active;
import com.sw.model.ActiveEnroll;
import com.sw.model.ActiveEnrollExample;
import com.sw.model.ActiveExample;
import com.sw.service.ActiveService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * Created by Administrator on 2017/7/12.
 */
@RestController
@RequestMapping(value = "activities")
public class ActiveController {
    protected Logger log= LoggerFactory.getLogger(getClass());
    Gson gson = new Gson();

    @Autowired
    private ActiveEnrollMapper activeEnrollMapper;
    @Autowired
    private ActiveMapper activeMapper;
    @Autowired
    private OriginatorMapper originatorMapper;
    @Autowired
    private ActiveService activeService;

    /**
     * 创建活动接口
     * @param active
     * @return
     */
    @RequestMapping(value = "" ,method = RequestMethod.POST)
//    @RequestBody  @RequestParam  @PathVariable
    public Active insert(@RequestBody  Active active){
        int timestamp=(int)(new Date().getTime()/100);
        active.setActiveId(timestamp);
        activeMapper.insertSelective(active);
        return active;
    }

    /**
     * 删除活动接口
     * @param activeid
     */
    @RequestMapping(value = "{activeid}" ,method = RequestMethod.DELETE)
    public Active delete(@PathVariable int activeid){
        Active active=activeMapper.selectByPrimaryKey(activeid);
        if (active==null){
            throw new InvalidArgumentException("该活动不存在，请确认activeid无误");
        }else {
            active.setActiveState("4");
            activeMapper.updateByPrimaryKey(active);
            return activeMapper.selectByPrimaryKey(activeid);
        }
    }

    /**
     * 根据活动ID修改活动接口
     * @param activeid
     * @param active
     */
    @RequestMapping(value = "{activeid}",method = {RequestMethod.POST, RequestMethod.PUT})
    public Active update(@PathVariable int activeid, @RequestBody Active active){
        if (activeMapper.selectByPrimaryKey(activeid)==null){
            throw new InvalidArgumentException("该活动不存在，请确认activeid无误");
        }else {
            active.setActiveId(activeid);
            if (active.getActiveState()=="4"){
                active.setActiveState(activeMapper.selectByPrimaryKey(activeid).getActiveState());
            }
            activeMapper.updateByPrimaryKey(active);
            return activeMapper.selectByPrimaryKey(activeid);
        }
    }

    /**
     * 根据活动ID查看活动接口
     * @param activeid
     * @return
     */
    @RequestMapping(value = "{activeid}",method = RequestMethod.GET)
    public Active get(@PathVariable int activeid){
        Active active = activeMapper.selectByPrimaryKey(activeid);
        if (active==null){
            throw new InvalidArgumentException("该活动不存在，请确认activeid无误");
        }else {
            return activeMapper.selectByPrimaryKey(activeid);
        }
    }

    /**
     * 获取活动列表(可根据组织者ID、状态进行筛选)
     * @param originatorId
     * @param activeState
     * @param pageNum
     * @return
     */
    @RequestMapping(value = "",method = RequestMethod.GET)
    public FullActivePage fullActivePageByOriginatorOrState(@RequestParam(required = false) Integer originatorId, @RequestParam(required = false) String activeState, @RequestParam(required = false) Integer pageNum){
        // 构造查询条件
        ActiveExample example=new ActiveExample();
        ActiveExample.Criteria criteria =  example.createCriteria();
        if (originatorId != null) {
            criteria.andOriginatorIdEqualTo(originatorId);
        }
        if (activeState != null) {
            criteria.andActiveStateEqualTo(activeState);
        }
        // 查询数据
        if (pageNum!=null){
            PageHelper.startPage(pageNum, 15);
        }
        List<Active> activeList = activeMapper.selectByExample(example);
        log.debug("activeList={}",gson.toJson(activeList));
        return activeService.getFullActivePage(activeList);
    }

    /**
     * 根据用户ID、用户报名状态获取活动列表
     * @param userId
     * @param enrollState
     * @param pageNum
     * @return
     */
    @RequestMapping(value ="byUserEnrollState/{userId}",method = RequestMethod.GET)
    public FullActivePage  fullActivePageByUserEnrollState(@PathVariable int userId,@RequestParam(required = false) Integer pageNum,@RequestParam(required = false) Integer enrollState){

        //配置
        List<Active> activeList1 = new ArrayList<>();
        List<Active> activeList2 = new ArrayList<>();

        // 拿到用户的活动报名表
        ActiveEnrollExample example=new ActiveEnrollExample();
        example.createCriteria().andUserIdEqualTo(userId);
        List<ActiveEnroll> activeEnrollList= activeEnrollMapper.selectByExample(example);

        // 拿到全部活动列表
        ActiveExample example1=new ActiveExample();
        example1.createCriteria().andActiveStateEqualTo("1");
        List<Active> activeList=activeMapper.selectByExample(example1);

        if (enrollState==null){
            activeList1=activeList;
        }else {
            switch (enrollState){
                case 0:
                    activeList1=activeList;
                    break;
                case 1:
                    for (int i=0;i<activeList.size();i++){
                        boolean bl=true;
                        for (int j=0;j<activeEnrollList.size();j++)
                        {
                            if (activeList.get(i).getActiveId() == activeEnrollList.get(j).getActiveId()){
                                bl=false;
                            }
                        }
                        if (bl){
                            activeList1.add(activeList.get(i));
                        }
                    }
                    break;
                case 2:
                    for (int i = 0; i < activeList.size();i++) {
                        boolean bl = false;
                        for (int j = 0; j < activeEnrollList.size(); j++) {
                            if (activeList.get(i).getActiveId() == activeEnrollList.get(j).getActiveId()) {
                                bl = true;
                            }
                        }
                        if (bl) {
                            activeList1.add(activeList.get(i));
                        }
                    }
                    break;
                default:
                    break;
            }
        }
        if (pageNum!=null){
            if (activeList1.size()>=pageNum*15){
                activeList2= activeList1.subList((pageNum-1)*15,pageNum*15);
            }else {
                activeList2= activeList1.subList((pageNum-1)*15,activeList1.size());
            };
        }else activeList2=activeList1;
        return activeService.getFullActivePage(activeList2);
    }
}


